package com.xyht.medicine_management_system.modules.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.modules.user.entity.MmsRole;
import com.xyht.medicine_management_system.modules.user.entity.req.MmsRoleReq;
import com.xyht.medicine_management_system.modules.user.entity.resp.MmsRoleResp;
import com.xyht.medicine_management_system.modules.user.mapper.MmsRoleMapper;
import com.xyht.medicine_management_system.modules.user.service.MmsRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.medicine_management_system.modules.user.constant.IsSysConstant.IS_SYS_FALSE;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-11-12
 */
@Service
public class MmsRoleServiceImpl extends ServiceImpl<MmsRoleMapper, MmsRole> implements MmsRoleService {
    @Resource
    private MmsRoleMapper mmsRoleMapper;

    @Override
    public ResponseResult addRole(MmsRoleReq mmsRoleReq) {
        if (isNullOrEmpty(mmsRoleReq.getRoleName())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsRole mmsRole = mmsRoleMapper.selectOne(new LambdaQueryWrapper<MmsRole>()
                .eq(MmsRole::getRoleName, mmsRoleReq.getRoleName()));
        if (mmsRole != null) {
            return CommonResult.failed(CommonCodeEnum.ROLE_EXIST);
        }

        MmsRole addRole = new MmsRole();
        addRole.setRoleName(mmsRoleReq.getRoleName());
        addRole.setIsSys(IS_SYS_FALSE);
        mmsRoleMapper.insert(addRole);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deleteRole(MmsRoleReq mmsRoleReq) {
        if (isNullOrEmpty(mmsRoleReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        mmsRoleMapper.deleteById(mmsRoleReq.getId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateRole(MmsRoleReq mmsRoleReq) {
        if (isNullOrEmpty(mmsRoleReq.getId(), mmsRoleReq.getRoleName())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsRole mmsRole = mmsRoleMapper.selectById(mmsRoleReq.getId());
        if (mmsRole == null) {
            return CommonResult.failed(CommonCodeEnum.ROLE_NOT_EXIST);
        }

        if (mmsRole.getRoleName().equals(mmsRoleReq.getRoleName())) {
            return CommonResult.success();
        }

        MmsRole role = mmsRoleMapper.selectOne(new LambdaQueryWrapper<MmsRole>()
                .eq(MmsRole::getRoleName, mmsRoleReq.getRoleName())
                .ne(MmsRole::getId, mmsRoleReq.getId()));
        if (role != null) {
            return CommonResult.failed(CommonCodeEnum.ROLE_EXIST);
        }

        MmsRole updateRole = new MmsRole();
        updateRole.setId(mmsRoleReq.getId());
        updateRole.setRoleName(mmsRoleReq.getRoleName());
        mmsRoleMapper.updateById(updateRole);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getRoleList(String searchStr, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<MmsRole> queryWrapper = new LambdaQueryWrapper<>();
        if (!isNullOrEmpty(searchStr)) {
            queryWrapper.like(MmsRole::getRoleName, searchStr);
        }
        queryWrapper.orderByDesc(MmsRole::getCreateTime);
        Page<MmsRole> page = new Page<>(pageNum, pageSize);
        mmsRoleMapper.selectPage(page, queryWrapper);
        List<MmsRoleResp> list = page.getRecords()
                .stream()
                .map(mmsRole -> {
                    MmsRoleResp roleResp = new MmsRoleResp();
                    BeanUtils.copyProperties(mmsRole, roleResp);
                    return roleResp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(list,total);
    }

    @Override
    public ResponseResult getRoleDetail(String roleId) {
        if (isNullOrEmpty(roleId)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsRole mmsRole = mmsRoleMapper.selectById(roleId);
        if (mmsRole == null){
            return CommonResult.failed(CommonCodeEnum.ROLE_NOT_EXIST);
        }

        MmsRoleResp roleResp = new MmsRoleResp();
        BeanUtils.copyProperties(mmsRole, roleResp);
        return CommonResult.success(roleResp);
    }
}
